//
//  YXYNineImageTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYNineImageTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "YXYImageCollectionViewCell.h"
@implementation YXYNineImageTableViewCell

-(void)dealloc {
    [_imageViews release];
    [_imageArrays release];
    [_title release];
    [_name release];
    [_desc release];
    [_ts release];
    [_sbjName release];
    [_itemName release];
    [_array release];
    [_from release];
    [_fnum release];
    [_cnum release];
    [_homeModel release];
    [_dataSource release];
    [_briImage release];
    [_focus release];
    [_review release];
    [_flowLayout release];
    [_collectionView release];
    _nineSkipDelegate = nil;
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [self addSubview:_collectionView];
        [_collectionView registerClass:[YXYImageCollectionViewCell class] forCellWithReuseIdentifier:@"YXYImageCollectionViewCellIdentifier"];
        [_flowLayout release];
        [_collectionView release];
        
        self.title = [[UILabel alloc] initWithFrame:CGRectZero];
        _title.font = [UIFont systemFontOfSize:15];
        [self addSubview:_title];
        [_title release];
        
        self.ts = [[UILabel alloc] initWithFrame:CGRectZero];
        _ts.font = [UIFont systemFontOfSize:10];
        _ts.textColor = [UIColor grayColor];
        [self addSubview:_ts];
        [_ts release];
        
        self.name = [[UILabel alloc] initWithFrame:CGRectZero];
        _name.font = [UIFont systemFontOfSize:10];
        _name.backgroundColor = [UIColor redColor];
        _name.textColor = [UIColor colorWithRed:240 / 256.0 green:255 / 256.0 blue:255 / 256.0 alpha:1];
        _name.textAlignment = 1;
        [self addSubview:_name];
        [_name release];
        
        self.sbjName = [[UILabel alloc] initWithFrame:CGRectZero];
        _sbjName.font = [UIFont systemFontOfSize:8];
        _sbjName.textColor = [UIColor blackColor];
        [self addSubview:_sbjName];
        [_sbjName release];
        
        self.desc = [[UILabel alloc] initWithFrame:CGRectZero];
        _desc.font = [UIFont systemFontOfSize:10];
        _desc.numberOfLines = 2;
        _desc.textColor = [UIColor grayColor];
        [self addSubview:_desc];
        [_desc release];
        
        self.from = [[UILabel alloc] initWithFrame:CGRectZero];
        _from.font = [UIFont systemFontOfSize:7];
        _from.textColor = [UIColor grayColor];
        [self addSubview:_from];
        [_from release];
        
        self.fnum = [[UILabel alloc] initWithFrame:CGRectZero];
        _fnum.font = [UIFont systemFontOfSize:8];
        _fnum.textColor = [UIColor grayColor];
        [self addSubview:_fnum];
        [_fnum release];
        
        self.cnum = [[UILabel alloc] initWithFrame:CGRectZero];
        _cnum.font = [UIFont systemFontOfSize:8];
        _cnum.textColor = [UIColor grayColor];
        [self addSubview:_cnum];
        [_cnum release];
        
        self.itemName = [[UILabel alloc] initWithFrame:CGRectZero];
        _itemName.font = [UIFont systemFontOfSize:12];
        _itemName.textColor = [UIColor whiteColor];
        [self addSubview:_itemName];
        [_itemName release];
        
        self.focus = [[UIImageView alloc] initWithFrame:CGRectZero];
        _focus.image = [UIImage imageNamed:@"focus"];
        [self addSubview:_focus];
        [_focus release];
        
        self.review = [[UIImageView alloc] initWithFrame:CGRectZero];
        _review.image = [UIImage imageNamed:@"review"];
        [self addSubview:_review];
        [_review release];
    }
    return self;
}




-(void)setHomeModel:(HomeModel *)homeModel {
    if (_homeModel != homeModel) {
        [_homeModel release];
        _homeModel = [homeModel retain];
    }
    //当数组有元素时全部删除，防止重复添加
    if (_array.count) {
        [_array removeAllObjects];
    }
    for (int i = 0; i < homeModel.dataSource.count; i++) {
        PictureModel *pictureModle = homeModel.dataSource[i];
        [self.array addObject:pictureModle.icon[@"url"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_collectionView reloadData];
        });
        self.fnum.text = [NSString stringWithFormat:@"%@", pictureModle.fnum];
        self.cnum.text = [NSString stringWithFormat:@"%@", pictureModle.cnum];
    }
    
    _desc.text = homeModel.desc;
    [_desc nightMode];
    _name.text = homeModel.name;
    [_name nightMode];
    _sbjName.text = homeModel.sbj[@"name"];
    [_sbjName nightMode];
    _ts.text = homeModel.ts;
    [_ts nightMode];
    _title.text = homeModel.title;
    [_title nightMode];
    _from.text = @"from";
    [_from nightMode];
    
    [_collectionView nightMode];
}
-(NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray arrayWithCapacity:0];
    }
    return _array;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    _imageViews.frame = self.bounds;
    
    _name.frame = CGRectMake(10, 0, 43, 15);
    _from.frame = CGRectMake(_name.frame.origin.x + 48, _name.frame.origin.y, 50, 15);
    _sbjName.frame = CGRectMake(_name.frame.origin.x + 70, _name.frame.origin.y, 50, 15);
    _title.frame = CGRectMake(10, self.frame.size.height - 90, self.frame.size.width, 20);
    _desc.frame = CGRectMake(12, self.frame.size.height - 70, self.frame.size.width - 2, 40);
    _ts.frame = CGRectMake(self.frame.size.width - 100, self.frame.size.height - 30, 90, 20);
    _fnum.frame = CGRectMake(_title.frame.origin.x + 20, self.frame.size.height - 30, 100, 20);
    _cnum.frame = CGRectMake(_fnum.frame.origin.x + 60, _fnum.frame.origin.y, 100, 20);
    _itemName.frame = CGRectMake(10, self.frame.size.height - 120, self.frame.size.width, 10);
    _focus.frame = CGRectMake(_fnum.frame.origin.x - 12, _fnum.frame.origin.y + 5, 10, 10);
    _review.frame = CGRectMake(_cnum.frame.origin.x - 12, _cnum.frame.origin.y + 5, 10, 10);
    _flowLayout.itemSize = CGSizeMake(self.frame.size.width / 3, (self.frame.size.height - 120) / 3);
    _flowLayout.minimumInteritemSpacing = 0;
    _flowLayout.minimumLineSpacing = 0;
    _collectionView.frame = CGRectMake(0, 20, self.frame.size.width, self.frame.size.height - 120);
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YXYImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YXYImageCollectionViewCellIdentifier" forIndexPath:indexPath];
    [cell.images sd_setImageWithURL:[NSURL URLWithString:_array[indexPath.item]]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureModel *pictureModle = _homeModel.dataSource[indexPath.row];
    [self.nineSkipDelegate nineImagePressSkipWithURL:pictureModle.next];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
